
import pymysql


def getData():
    with open("nick1.txt", encoding='utf-8', mode = 'r') as f:  # 打开文件
        data = f.read()  # 读取文件
        str_list = data.split()
        print(str_list)
        addDB(str_list)

def addDB(str_list):
    # 打开数据库连接，不指定数据库
    conn = pymysql.connect('localhost', 'root', 'xuhuan.5130638')
    conn.select_db('aweiliao')
    cur = conn.cursor()  # 获取游标

    try:
        # print(jsonss)
        for item in str_list:
            # 这部分可以循环执行
            sql = "insert into sys_nick (nick,sex) values('{}','{}')".format(
                item, 0
            )
            print(len(sql))
            insert = cur.execute(sql)
            conn.commit()
            print('添加语句受影响的行数：', insert)
    finally:
        cur.close()
        conn.close()
        print('sql执行成功')
if __name__ == '__main__':
    getData()